#include <func.h>
#include "head.h"

int levelOrderTranversal(MYSQL *db, int curPathId, int netFd);
int removeFile(MYSQL *db, char *argv, char *curPath, char *userName, int netFd){
    //remove /user1/file
    //pwd /usr1
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[200] = {0};
    // puts(filePath);
    // printf("curPath = %s\n", curPath);
    if(filePath[0] == '.'){
        // if(filePath[1] == '.'){
        //     // cd ..
        //     int preId = findParentIdByfileName(db, curPath);
        //     printf("preId = %d\n", preId);
        //     char PrePath[200];
        //     int res = findFileNameById(db, preId, PrePath);
        //     if(res == -1){
        //         send(netFd, "1", 1, MSG_NOSIGNAL);
        //         return -1;
        //     }
        //     strcpy(realPath, PrePath);
        // }else 
        if(filePath[1] == '/'){
            //rm ./dir1
            sprintf(realPath, "%s%s", curPath, filePath+1);
        }else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }else if(filePath[0] == '/'){
        //rm /user1/dir1
        puts("/");
        strcpy(realPath, filePath);
    }else{
        //rm dir 
        sprintf(realPath, "%s%s%s", curPath, "/", filePath);
    }

    // printf("realPath = %s\n", realPath);
    int ret = isFileByfileName(db, realPath, userName);
    if(ret == 0){
        // puts("isFile");
        deleteFilebyfileName(db, realPath, userName);
        send(netFd, "0", 1, MSG_NOSIGNAL);
        return 0;
    }else{
        // puts("isDir");
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
}

/*cd还需优化功能：
    1. cd .., ./
    2. 禁止用户cd到其他用户目录下
  已优化*/
int cd(MYSQL *db, char *argv, char *curPath, char *userName, int netFd){
    // cd /xxx/xxx/xxx
    //cd /usr1/dir1
    //pwd /usr1
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[200] = {0};
    // puts(filePath);
    // printf("curPath = %s\n", curPath);
    if(filePath[0] == '.'){
        if(filePath[1] == '.'){
            // cd ..
            int preId = findParentIdByfileName(db, curPath);
            // printf("preId = %d\n", preId);
            char PrePath[200];
            int res = findFileNameById(db, preId, PrePath);
            if(res == -1){
                send(netFd, "1", 1, MSG_NOSIGNAL);
                return -1;
            }
            strcpy(realPath, PrePath);
        }else if(filePath[1] == '/'){
            //cd ./
            sprintf(realPath, "%s%s", curPath, filePath+1);
        }else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }else if(filePath[0] == '/'){
        //cd /user1/dir1
        // puts("/");
        strcpy(realPath, filePath);
    }else{
        //cd dir 
        sprintf(realPath, "%s%s%s", curPath, "/", filePath);
    }

    // printf("realPath = %s\n", realPath);
    int ret = isDirectoryByfileName(db, realPath, userName);
    if(ret == 0){
        send(netFd, "0", 1, MSG_NOSIGNAL);
        strcpy(curPath, realPath);
        // printf("curPath = %s\n", curPath);
        return 0;
    }else{
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
}

int ls(MYSQL *db, int curPathId, int netFd){
    char childrenName[500] = {0};
    findChildBycurPathId(db, curPathId, childrenName);
    // printf("childrenName = %s\n", childrenName);
    train_t train;
    strcpy(train.buf, childrenName);
    train.dataLength = strlen(train.buf);
    send(netFd, &train, sizeof(train.dataLength) + train.dataLength, MSG_NOSIGNAL);
    return 0;
}

int pwd(char *pwd, int netFd){
    train_t train;
    strcpy(train.buf, pwd);
    train.dataLength = strlen(train.buf);
    send(netFd, &train, sizeof(train.dataLength) + train.dataLength, MSG_NOSIGNAL);
    return 0;
}

int makedir(MYSQL *db, char *argv,char *curPath, int curPathId, char *userName, int netFd){
    //mkdir dir1
    char arr[100] = "";
    strcpy(arr, argv);
    char *word = strtok(arr," ");
    char *dirName = strtok(NULL," ");
    char realPath[200] = {0};
    sprintf(realPath, "%s%s%s", curPath, "/", dirName); 
    puts(realPath);

    //在数据库中插入一条目录数据
    int ret = insertDir(db, realPath, userName, curPathId);
    if(ret == -1){
        send(netFd, "0", 1, MSG_NOSIGNAL);
    }else{
        send(netFd, "1", 1, MSG_NOSIGNAL);
    }
}

int removedir(MYSQL *db, char *commandLine, char *curPath, char *userName, int netFd){
    // rmdir 
    char arr[100] = "";
    strcpy(arr, commandLine);
    char *word = strtok(arr," ");
    char *filePath = strtok(NULL," ");
    char realPath[200] = {0};
    // puts(filePath);
    // printf("curPath = %s\n", curPath);
    if(filePath[0] == '.'){
        // if(filePath[1] == '.'){
        //     // cd ..
        //     int preId = findParentIdByfileName(db, curPath);
        //     printf("preId = %d\n", preId);
        //     char PrePath[200];
        //     int res = findFileNameById(db, preId, PrePath);
        //     if(res == -1){
        //         send(netFd, "1", 1, MSG_NOSIGNAL);
        //         return -1;
        //     }
        //     strcpy(realPath, PrePath);
        // }else 
        if(filePath[1] == '/'){
            //rmdir ./dir1
            sprintf(realPath, "%s%s", curPath, filePath+1);
        }else{
            send(netFd, "1", 1, MSG_NOSIGNAL);
            return -1;
        }
    }else if(filePath[0] == '/'){
        //rmdir /user1/dir1
        puts("/");
        strcpy(realPath, filePath);
    }else{
        //rmdir dir 
        sprintf(realPath, "%s%s%s", curPath, "/", filePath);
    }

    // printf("realPath = %s\n", realPath);
    int ret = isDirectoryByfileName(db, realPath, userName);
    if(ret == 0){
        // puts("isFile");
        
        //层序遍历删除
        int id = findIdByfileName(db, realPath);
        levelOrderTranversal(db, id, netFd);

        send(netFd, "0", 1, MSG_NOSIGNAL);
        return 0;
    }else{
        // puts("isDir");
        send(netFd, "1", 1, MSG_NOSIGNAL);
        return -1;
    }
}

int levelOrderTranversal(MYSQL *db, int curPathId, int netFd){
    idQueue_t *pIdQueue = (idQueue_t *)calloc(1, sizeof(idQueue_t));
    initIdQueue(pIdQueue);
    idEnQueue(pIdQueue, curPathId);
    while(pIdQueue->queueSize != 0){
        //取出队列第一个，保存id, sql删除该id列
        int id = idDeQueue(pIdQueue);
        deletebyid(db, id);
        
        //sql找到父节点序号为id的节点，入队
        char sqlCommand[1024] = {0};
        sprintf(sqlCommand, "select id from catalog where pre_id = %d;", id);
        //puts(sqlCommand);
        int qret = mysql_query(db, sqlCommand);
        if(qret != 0){
            printf("Error: %s\n", mysql_error(db));
            return -1;
        }
        MYSQL_RES *result = mysql_store_result(db);
        MYSQL_ROW row = mysql_fetch_row(result);
        if(row == NULL){
            continue;
        }
        do{
            idEnQueue(pIdQueue, atoi(row[0]));
        }while((row = mysql_fetch_row(result)) != NULL);
        mysql_free_result(result);
    }

    return 0;
}
